package com.auth.secruity;

import com.auth.pojo.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/7/21 0021.
 * jwtUser工厂类
 */
public final class JwtUserFactory {
    private JwtUserFactory() {
    }

    /**
     * 创建一个JwtUser
     * @param user
     * @return
     */
    public static JwtUser create(User user){
        return new JwtUser(
                user.getId(),
                user.getUserName(),
                user.getPassWord(),
                user.getEmail(),
                mapToGrantedAuthorities(user.getRoles()),
                user.getLastPassWordResetDate()
        );
    }

    /**
     * List<String>转换成List<GrantedAuthority>
     * @param authorities
     * @return
     */
    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}
